package com.example.auto_space;

import java.util.ArrayList;

import com.example.auto_space.SelectSpace.ListItem;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.ListView;
import android.widget.Switch;
import android.widget.TextView;

public class EditDevice extends Activity {
	
	//listview
	private ListView activeListView;
	private ListView inactiveListView;
	//item array
	private ArrayList<ListItem> activeItems = new ArrayList<ListItem>();
	private ArrayList<ListItem> inactiveItems = new ArrayList<ListItem>();
	
	private ListItem mi;	
	private MyListAdapter activeAdapter;
	private MyListAdapter inactiveAdapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_edit_device);
		
		//get value
		Intent intent = getIntent();
		String value = intent.getStringExtra("ITEM_NAME");

		//set (item Name)
		TextView itemName = (TextView)findViewById(R.id.editdevice_itemname_textview);
		itemName.setText(value);
		
		//active list init
		mi = new ListItem("Stand", true);
		activeItems.add(mi);
		mi = new ListItem("BLight", true);
		activeItems.add(mi);		
		mi = new ListItem("SLight", false);
		activeItems.add(mi);
		//inactive list init
		mi = new ListItem("MLight", true);
		inactiveItems.add(mi);
		mi = new ListItem("LLight", false);
		inactiveItems.add(mi);
		mi = new ListItem("Light", false);
		inactiveItems.add(mi);
		
		//set activeItems adapter
		activeAdapter = new MyListAdapter(this, R.layout.list_custom_2, activeItems);
		activeListView = (ListView)findViewById(R.id.editdevice_activelist);
		activeListView.setAdapter(activeAdapter);
		activeListView.setOnItemClickListener(new AdapterView.OnItemClickListener()
		{
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id)
            {
                ListView listView = (ListView) parent;
                //get items
                String item = (String) listView.getItemAtPosition(position);
                Log.d("Selected Item", item);

                //add item
                mi = new ListItem(activeItems.get(position).text, activeItems.get(position).isOn);
                inactiveItems.add(mi);
                inactiveAdapter.notifyDataSetChanged();
                //delete item (position)
                for(int i = position; i < activeItems.size()-1; i++)
                {
                	activeItems.set(i, activeItems.get(i+1));
                }
				int delPos = activeItems.size()-1;
		        if (delPos < 0) return;
		        activeItems.remove(delPos);
		        activeAdapter.notifyDataSetChanged();
            }
        });
		//set inactiveItems adapter
		inactiveAdapter = new MyListAdapter(this, R.layout.list_custom_2, inactiveItems);
		activeListView = (ListView)findViewById(R.id.editdevice_inactivelist);
		activeListView.setAdapter(inactiveAdapter);
		activeListView.setOnItemClickListener(new AdapterView.OnItemClickListener()
		{
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id)
            {
                ListView listView = (ListView) parent;
                //get items
                String item = (String) listView.getItemAtPosition(position);
                Log.d("Selected Item", item);
                
                //add item
                mi = new ListItem(inactiveItems.get(position).text, inactiveItems.get(position).isOn);
                activeItems.add(mi);
                activeAdapter.notifyDataSetChanged();
                //delete item (position)
                for(int i = position; i < inactiveItems.size()-1; i++)
                {
                	inactiveItems.set(i, inactiveItems.get(i+1));
                }
				int delPos = inactiveItems.size()-1;
		        if (delPos < 0) return;
		        inactiveItems.remove(delPos);
		        inactiveAdapter.notifyDataSetChanged();
            }
        });
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.edit_device, menu);
		return true;
	}
	
	//listview's items
	class ListItem
	{
		String text;	//text
		boolean isOn;	//switch
		
		ListItem(String text, boolean isOn)
		{
			this.text = text;
			this.isOn = isOn;
		}
	}
	
	//adapter class
	class MyListAdapter extends BaseAdapter
	{
		Context maincon;
		LayoutInflater inflater;
		ArrayList<ListItem> arSrc;
		int layout;

		public MyListAdapter(Context context, int alayout, ArrayList<ListItem> aarSrc)
		{
			maincon = context;
			arSrc = aarSrc;
			layout = alayout;
			// for load xml, create LayoutInfalater
			inflater = LayoutInflater.from(maincon);
		}
		 
		public int getCount()
		{
			return arSrc.size();
		}
		 
		public Object getItem(int position)
		{
			return arSrc.get(position).text;
		}
		 
		public long getItemId(int position)
		{
			return position;
		}
		 
		//create views
		@Override
		public View getView(int position, View convertView, ViewGroup parent)
		{
			final int pos = position;
			if (convertView == null)
			{
				convertView = inflater.inflate(layout, parent, false);
			}
			
			TextView txt = (TextView)convertView.findViewById(R.id.editdevice_textview);
			txt.setText(arSrc.get(pos).text);
			
			Switch sw = (Switch)convertView.findViewById(R.id.editdevice_switch);
			sw.setChecked(arSrc.get(pos).isOn);
			sw.setOnCheckedChangeListener(new Switch.OnCheckedChangeListener()
	        {
				@Override
				public void onCheckedChanged(CompoundButton buttonView,
						boolean isChecked) {
					// TODO Auto-generated method stub
					//switch on off <<
				}
	        	
	        });
			return convertView;
		}
	
	}

}
